<?php
namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Models\Article;
use App\Http\Models\Cate;
use Illuminate\Support\Facades\Session;
class IndexController extends Controller{


	//登录实现
	public function login()
	{	
		session::start();
		return view('index/index.login');
	}

	//登录处理
	public function do_login(Request $request)
	{	
		session::start();
		$gm_name = $request->name;
		$gm_pwd = $request->pwd;

		$result = DB::table('gm')->where(['gm_name'=>$gm_name ,'gm_pwd'=>$gm_pwd])->first();

		if($result){

			Session::put('id',$result->id);
			Session::put('gm_name',$result->gm_name);
			Session::put('gm_pwd',$result->gm_pwd);
			/*$res = Session::all();
			var_dump($res);die;*/
			return redirect('index/list');

		}else{

			return redirect('index/login');
		}
	}

	//注销
	public function logout(Request $request)
	{	
		session::start();
		if ($request->session()->has('gm_name')) {
			$is_forgotten = $request->session()->forget('gm_name');
				if ($is_forgotten === null)
					return view('index/index.login');
		}else{
			return view('index/index.list');
	
		}
	}	
	//后台首页
	public function list(Request $request)
	{	
		session::start();
		if ($request->session()->has('gm_name'))
		{	
			$result = $request->session()->get('gm_name');
			//echo $result;die;
			return view('index/index.list',['result'=>$result]);
		}else{
			return view('index/index.login');
		}
	}

	//后台列表
	public function introduce()
	{	
		session::start();
		$result = Db::table('gm')->select('gm.gm_name','gm.gm_pwd','gm.gm_email','group.group','gm.*')

                        ->join('group',function($join){
                                   $join->on('gm.g_id','=','group.g_id');
                        })
                        ->paginate(4);
       // print_r($result);die;

		//$result = DB::table('gm')->paginate(1);

		return view('index/index.introduce',['result'=>$result]);
	}

	//状态处理
    public function intro_do(Request $request){
    	session::start();
       	$status = $request->status;
       	$id = $request->id;
         if($status == 1){
             $res = DB::table('gm')
                 ->where('id','=', $id)
                 ->update(['status' => 0]);
         }else{
             $res = DB::table('gm')
                 ->where('id', $id)
                 ->update(['status' => 1]);
         }
        if($res){
            return 1;
        }else{
            return 0;
        }
    }

    //删除处理
    public function del_do(Request $request)
    {	
    	
    	session::start();
    	$id = $request->id;
    	//print_r($id);die;
    	$res = Db::table('gm')->where('id','=',$id)->delete();

    	if($res){
    		return 1;
    	}else{
    		return 0;
    	}
    }

    //添加
    public function add()
    {
    	session::start();
    	return view('index/index.add');
    }
    //添加处理
    public function add_do(Request $request)
    {
    	session::start();
    	$gm_name = $request->gm_name;
    	//print_r($gm_name);die;
    	$gm_pwd = $request->gm_pwd;
    	$gm_email = $request->gm_email;
    	$gm_tel = $request->gm_tel;
    	$g_id = $request->g_id;
    	$result = Db::table('gm')->insert([
    		'gm_name' => $gm_name,
    		'gm_pwd' => $gm_pwd,
    		'gm_email' => $gm_email,
    		'gm_tel' =>$gm_tel,
    		'g_id' => $g_id,
    	]);
    	//alert('55');die;
    	//print_r($result);die;
    	if($result == 1){
    		return "<script>alert('添加成功！');window.location.href='/index/introduce'</script>";
    	}else{
    		return "<script>alert('添加失败！');window.location.href='/index/add'</script>";
    	}

    }
	public function house(Request $request)
    {
        session_start();
        if (Session::has('gm_name')) {
            $res = Session::all();
            $sale_property_id = $res['id'];
            $data = DB::table('house')->where([
                ['property_id', '=', $sale_property_id],
            ])->get();
            return view('admin/property.houselist',['list'=>$data]);
        }else{
            return view('admin/login.index');
        }

    }
    public function houseadd(Request $request){
        session_start();
        if (Session::has('gm_name')) {
            $res = DB::table('sale_user')->get();
            return view('admin/property.houseadd', ['data' => $res]);
        }
    }
    public function review(Request $request){
        session_start();
        if (Session::has('gm_name')) {
            $res = Session::all();
            $sale_property_id = $res['id'];
            $data = DB::table('sale_user')->where([
                ['property_id', '=', $sale_property_id],
            ])->get();
            return view('admin/property.review',['list'=>$data]);
        }else{
            return view('admin/login.index');
        }
    }


}